import express from "express";
const router = express.Router();
let MD5 = require("../../libs/MD5.js");
let axios = require("axios");
let qs = require("qs");
router.post("/trans", (req, res) => {
  if (req.body.tranContent === "") {
    res.send({ errorCode: 400, msg: "翻译内容不能为空" });
  }
  if (!req.body.lan) req.body.lan = "zh";
  let appid = "20181114000234282";
  let key = "NSnJTT5W5vyBvrPMVV8H";
  let salt = new Date().getTime();
  let query = req.body.tranContent;
  // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
  let from = "auto";
  let to = req.body.lan;
  let str1 = appid + query + salt + key;
  let sign = MD5(str1);
  let data = {
    q: query,
    appid: appid,
    salt: salt,
    from: from,
    to: to,
    sign: sign
  };
  axios
    .get(
      "http://api.fanyi.baidu.com/api/trans/vip/translate?" + qs.stringify(data)
    )
    .then(response => {
      res.send(response.data);
    });
});
module.exports = router;
